#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 2021-12-21,现在看这玩意写出来没用

from lib.Restful.hyhive import HttpHyHive
from lib.CLI.CmdWrapper import CmdWrapper
import traceback
from lib.Logger.LoggerBase import LoggerBase
from lib.WEBUI.WebTestBase import WebTestBase
from lib import glo
from lib.AppiumLib.WinAppDriver import winApp


# def hy_hooker(func):
#     def wrapper(*args, **kwargs):
#         try:
#             func(*args, **kwargs)
#         except Exception as e:
#             try:
#                 glo._global_dict["Logger"].warning(traceback.format_exc())
#                 glo._global_dict["Logger"].case_down()
#             except:
#                 pass
#             finally:
#                 raise Exception(traceback.format_exc())
#         else:
#             glo._global_dict["Logger"].case_down()
#
#     return wrapper


class AutoEngine():

    def __init__(self, **kwargs):
        self.bedDir = glo._global_dict["xml"]
        self.case_name = traceback.extract_stack()[-2][2]
        self.__log_initialization()

    def __log_initialization(self):
        name = self.bedDir["log_path"] + self.case_name
        log = LoggerBase()
        log._generate_FileHandler(name)
        glo.set_value('Logger', log)
        self.log = log

    def _init_win_app(self, desired_caps=None, host="localhost", port="4723"):
        #  windows 应用或 手机应用 需要初始化
        self.winapp = winApp(desired_caps, host, port)

    def _login_node(self, **kwargs):
        # ssh登录节点，根据传入登录n号节点，并保存channel
        id = kwargs.get("id", "1")
        node_info = self.bedDir["nodes"][id]
        node_info["log"] = glo._global_dict["Logger"]
        con = CmdWrapper(**node_info)
        channel_name = "channel" + id
        setattr(self, channel_name, con)

    def _generate_web_driver(self, **kwargs):
        host = kwargs.get("host", "1")
        node_info = self.bedDir["nodes"][host]
        node_info["log"] = glo._global_dict["Logger"]
        self.webdriver = WebTestBase(**node_info)

    def _generate_http_connector(self, **kwargs):
        host = kwargs.get("host", "1")
        node_info = self.bedDir["nodes"][host]
        node_info["log"] = glo._global_dict["Logger"]
        self.connector = HttpHyHive(**node_info)
        return True

    def execute_cmd(self, nodeID, cmd, timeout=60):
        attr_name = "channel" + nodeID
        channel = getattr(self, attr_name, None)
        if channel:
            res = channel.nodeshell.exe_cmd(cmd, timeout)
        else:
            try:
                self._login_node(id=nodeID)
            except Exception as e:
                raise e
            else:
                res = self.execute_cmd(nodeID, cmd, timeout)
        return res

    def wrapper_excute(self, nodeID, wrapperName, kargs):

        attr_name = "channel" + nodeID
        channel = getattr(self, attr_name, None)
        if channel:
            wrapper = getattr(channel, wrapperName, None)
            res = wrapper(**kargs)
        else:
            try:
                self._login_node(id=nodeID)
            except Exception as e:
                raise e
            else:
                wrapper = getattr(channel, wrapperName, None)
                res = wrapper(**kargs)
        return res
